<?php

/**
 * I_POST  获取post提交的参数
 * @param string $key
 * @return mixed
 * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
 */
function I_POST($key = '', $filter = 'text')
{
    $value = I('post.' . $key,'',$filter);
    if (empty($value)) {
        $value = I('put.' . $key,'',$filter);
    }

    return $value;
}


/**
 * api_encode  加密
 * @param $txt
 * @param null $key
 * @return string
 * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
 */
function api_encode($txt, $key = null)
{
    $key = empty($key) ? C('DATA_AUTH_KEY') : $key;
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_";
    $nh = rand(0, 64);
    $ch = $chars[$nh];
    $mdKey = md5($key . $ch);
    $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
    $txt = base64_encode($txt);
    $tmp = '';
    $i = 0;
    $j = 0;
    $k = 0;
    for ($i = 0; $i < strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = ($nh + strpos($chars, $txt [$i]) + ord($mdKey[$k++])) % 64;
        $tmp .= $chars[$j];
    }
    return $ch . $tmp;
}

/**
 * api_decode  解密
 * @param $txt
 * @param null $key
 * @return string
 * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
 */
function api_decode($txt, $key = null)
{
    $key = empty($key) ? C('DATA_AUTH_KEY') : $key;

    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_";
    $ch = $txt[0];
    $nh = strpos($chars, $ch);
    $mdKey = md5($key . $ch);
    $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
    $txt = substr($txt, 1);
    $tmp = '';
    $i = 0;
    $j = 0;
    $k = 0;
    for ($i = 0; $i < strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = strpos($chars, $txt[$i]) - $nh - ord($mdKey[$k++]);
        while ($j < 0) {
            $j += 64;
        }
        $tmp .= $chars[$j];
    }
    return base64_decode($tmp);
}

//function render_picture_path($path){
//    $path = get_pic_src($path);
//    return is_bool(strpos($path, 'http://')) ?  'http://'.str_replace('//','/',$_SERVER['HTTP_HOST'] .'/'. $path) : $path;
//}

function render_picture_path_without_root($path){
$path = get_pic_src($path);
   return is_bool(strpos($path, 'http://')) ?  'http://'.str_replace('//','/',$_SERVER['HTTP_HOST'] . $path) : $path;
}


function del_dir($dir)
{
    //先删除目录下的文件：
    $dh = opendir($dir);
    while ($file = readdir($dh)) {
        if ($file != "." && $file != "..") {
            $fullpath = $dir . "/" . $file;
            if (!is_dir($fullpath)) {
                unlink($fullpath);
            } else {
                $this->deldir($fullpath);
            }
        }
    }

    closedir($dh);
    //删除当前文件夹：
    if (rmdir($dir)) {
        return true;
    } else {
        return false;
    }
}



function get_from($from_en){
    switch($from_en){
        case 'MI 4LTE':$from = '小米4';break;
        case 'iPhone':$from = 'IPhone客户端';break;
        case 'HM 2A':$from = '红米2A';break;
        default :
            $from =$from_en;
            if(strpos($from_en,'H60') === 0){
                $from = '华为荣耀6';
            }

    }
    return $from;
}


function H5U($url = '', $vars = '', $suffix = true, $domain = false){
    $url = preg_replace("/(?<=#)[\s\S]*$/","",$url);
    $link =  require('./Application/Api/Conf/router.php');
    $url_mob = $link['router'][$url];
    return U($url_mob, $vars , $suffix, $domain);
}

